<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
date_default_timezone_set ( 'prc' );
class Newproduct extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		if (! $this->session->userdata ( 'logged_in' )) {
			redirect ( 'index_admin/login' );
			exit ();
		}
	}
	/*
	 *返回新品列表页面
	 */
	public function index() {
		$this->load->library ( 'pagination' );
		// 每页显示数据条数
		$per_page=20;//暂定为20条
		
		$config['base_url']=site_url('newproduct/index');
		$this->load->model ( 'newproduct_model' );
		// 数据总数
		$config ['total_rows'] = $this->newproduct_model->count_newproducts();
		//$config ['total_rows']=200;
		$config ['per_page'] = $per_page;
		$config ['first_link'] = '第一页';
		$config ['last_link'] = '最后一页';
		$config ['next_link'] = '下一页';
		$config ['prev_link'] = '上一页';
		$config['uri_segment'] =3;
		
		$this->pagination->initialize($config);
		$data['links']=$this->pagination->create_links();
		//偏移量
		$offset=intval($this->uri->segment(3));
		$data ['newproducts'] = $this->newproduct_model->find_all_newproducts ($per_page,$offset);
		$data ['title'] = '新品管理 - 集市宝仓库管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'newproduct/index' );
		$this->load->view ( 'footer' );
	}
	/*
	 *发送短信提醒用户新品上架
	 */
	public function sendmessage() {
		$userid = $this->input->post ( 'userid' );
		$this->load->model ( 'user_model' );
		$user=$this->user_model->load ( $userid );
		$sid="9f014d8f371d9a94348092594b971d3f";
		$appid="55953fa1a9014153b89e194bee63770a";
		$key="d4435599239290ed5e40df062cc85e37";
		$time=date("YmdHis").'111';
		$sign=md5($sid.$time.$key);
		$templateid="13398";
		$param=$user['shopname'];
		$phone = $user['cellphone'];
		$url="http://www.ucpaas.com/maap/sms/code?sid=".$sid."&appId=".$appid."&time=".$time."&sign=".$sign."&to=".$phone."&templateId=".$templateid."&param=".$param;
		$ch = curl_init ();
		curl_setopt ( $ch, CURLOPT_URL, $url );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $ch, CURLOPT_HEADER, 0 );
		$output = curl_exec ( $ch );
		curl_close ( $ch );
		$output = json_decode($output);
		if($output->resp->respCode=="000000"){//短信发送成功	
			$message = array (
					'phone' => $phone,
					'state' => 1,
					'smsid' => $output->resp->templateSMS->smsId,
					'createdate' => $output->resp->templateSMS->createDate
			);
			$this->load->model ( 'message_model' );
			$this->message_model->create($message);//更新短信表
			return_json ( TRUE, '', '发送成功！' );
		}else{
			return_json ( FALSE, '', '发送失败！' );
		}
	}
	/*
	 * 返回回复新品预定页面
	 */
	public function reply() {
		$id = $this->uri->segment(4);
		$this->load->model ( 'newproduct_model' );
		$data ['newproduct'] = $this->newproduct_model->load ( $id );
		$data ['title'] = '回复新品预定 - 集市宝仓库管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'newproduct/reply' );
		$this->load->view ( 'footer' );
	}
	/*
	 *保存建议信息
	 */
	public function save() {
		$id = $this->input->post('id');
		$newproduct = array (
				'reply' => $this->input->post ( 'content' )
		);
		$this->load->model ( 'newproduct_model' );
		if (!empty($id)) {
			if($this->newproduct_model->update ( $id, $newproduct )){
				return_json(TRUE,'','回复成功！'); 
			}else{
				return_json(FALSE,'','回复失败！');
			}
		} 
	}
}